<?php
/**
 * Created by PhpStorm.
 * User: vanity
 * Date: 2018/1/2
 * Time: 13:25
 */
namespace Admin\Controller;
use Think\Controller;
use Admin\Model\PermissionsModel;
class PermissionController extends CommonController{

    protected $fields = [
        'name'        => '',
        'label'       => '',
        'description' => '',
        'cid'         => 0,
        'icon'        => '',
    ];

    public function index()
    {
        $cid = $_REQUEST['cid']?$_REQUEST['cid']:0;
        $this->assign('cid',$cid);
        $map = array();
        $map['cid'] = $cid;
        $permission = M('permissions')->where($map)->select();
        $this->assign('permission',$permission);
        $this->display();
    }
    public function create()
    {
        $cid = $_REQUEST['cid']?$_REQUEST['cid']:0;
        $this->assign('cid',$cid);

        $data = [];
        foreach($this->fields as $field => $value){
            $data[$field] = $value;
        }
        $data['cid'] = $cid;
        $this->assign('data',$data);
        $this->display();
    }
    public function store()
    {
        $url = U('admin/permission/create',array('cid'=>$_POST['cid']));
        $permission = D('Permissions');
        if (!$permission->create()){
            $this->withError($permission->getError(),$url);
        }
        $res = $permission->add();
        if(!$res){
            $this->withError('添加失败',$url);
        }
        $this->withSuccess('添加成功',U('admin/permission/index',array('cid'=>$_POST['cid'])));
    }
    public function edit()
    {
        $id = intval($_REQUEST['id']);
        $permission = D('Permissions')->where('id = '.$id)->find();
        $data = [];
        foreach(array_keys($this->fields) as $field){
            $data[$field] = $permission[$field];
        }
        $data['id'] = $id;
        $this->assign('data',$data);
        $this->display();
    }
    public function update()
    {
        $url = U('admin/permission/edit',array('id'=>$_POST['id']));

        $permission = D('Permissions');
        $data = $permission->create($_POST,2);
        if(!$data){
            $this->withError($permission->getError(),$url);
        }
        $res = $permission->save();
        if(!$res){
            $this->withError('修改失败',$url);
        }
        $this->WithSuccess('修改成功',U('admin/permission/index',array('cid'=>$data['cid'])));
    }
    public function delete()
    {
        $id = intval($_POST['id']);
        $cid = intval($_POST['cid']);
        $url = U('admin/permission/index',array('cid'=>$cid));
        if($id == ''|| $id == 0){
            $this->withError('信息丢失！',$url);
        }
        $permission = D('Permissions');
        $res = $permission->where('cid = '.$id)->find();
        if($res){
            $this->withError('请先将该权限的子权限删除后再做删除操作!',$url);
        }
        $map['id'] = $id;
        $result = $permission->where($map)->relation('permission_role')->delete();
        if(!$result){
            $this->withError('删除失败',$url);
        }
        $this->withSuccess('删除成功',$url);
    }
}
